package com.shushan.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import com.android.fgchecker.AppChecker;
import com.shushan.base.Constants;
import com.shushan.home.TabHomeActivity;
import com.shushan.lockscreen.LockScreenWindowManager;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.util.SystemUtils;

/**
 * @ClassName: DialRTAService
 * @Desciption: 拨打电话监听服务
 * @author: yichaohua
 * @date: 2018-08-21
 */
public class DialRTAService extends Service {

    private final static String TAG = "DialRTAService";

    private String allowPackName = Constants.ALLOW_DIAL_PACK_NAME;
    private int rtaType = 2;//监控类型；1：锁屏忘记密码拨打客服 2：首页忘记密码拨打客服 3：管控模式下来电监听 4:支付监听
    private AppChecker appChecker;

    private String [] allowHuaweiPayPackName = {"com.tencent.mm","com.eg.android.AlipayGphone"};

    @Override
    public void onCreate() {
        super.onCreate();

        Log.i(TAG, "onCreate()");

        startChecker();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if(intent != null){
            rtaType = intent.getIntExtra("rtaType",2);
            allowPackName = intent.getStringExtra("allowPackName");
        }

        Log.i(TAG, "onStartCommand() rtaType=" + rtaType + " allowPackName=" + allowPackName);

        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        Log.i(TAG,"onDestroy()");

        stopChecker();
    }

    private void startChecker() {
        appChecker = new AppChecker();
        appChecker.when(getPackageName(), new AppChecker.Listener() {
            @Override
            public void onForeground(String packageName) {
                Log.i(TAG, "Our app is in the foreground.");
                BabyApplication babyApplication = BabyApplication.getInstance();
                if(rtaType == 1){
                    LockScreenWindowManager.createLockFloatWindow(babyApplication);

                    stopSelf();
                }else if (rtaType == 2){
                    SystemUtils.sendForegActivity(babyApplication,TabHomeActivity.class);

                    stopSelf();
                }else if (rtaType == 3){
                    TelephonyManager telMgr = (TelephonyManager) getSystemService(Service.TELEPHONY_SERVICE);
                    if(telMgr.getCallState() == TelephonyManager.CALL_STATE_IDLE){
                        Intent startIntent = new Intent(babyApplication, TabHomeActivity.class);
                        startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        babyApplication.startActivity(startIntent);

                        stopSelf();
                    }else {
                        SystemUtils.startApp(babyApplication,allowPackName);
                    }

                }else if (rtaType == 4){
                    stopSelf();
                }

            }
        })
        .whenOther(new AppChecker.Listener() {
            @Override
            public void onForeground(String packageName) {
                Log.i(TAG,"onForeground packageName=" + packageName + " rtaType=" + rtaType);
                if(allowPackName.equals(packageName) || SystemUtils.isSparePackName(packageName)){
                    return;
                }

                if(getPackageName().equals(Constants.HUAWEI_PACKAGE_NAME) && (rtaType == 4) && isHuaweiPay(packageName)){
                    return;
                }

                BabyApplication babyApplication = BabyApplication.getInstance();
                if(rtaType == 1){
                    LockScreenWindowManager.createLockFloatWindow(babyApplication);

                    stopSelf();
                }else if (rtaType == 2){
                    SystemUtils.sendForegActivity(babyApplication,TabHomeActivity.class);

                    stopSelf();
                }else if (rtaType == 3){
                    TelephonyManager telMgr = (TelephonyManager) getSystemService(Service.TELEPHONY_SERVICE);
                    if(telMgr.getCallState() == TelephonyManager.CALL_STATE_IDLE){
                        Intent startIntent = new Intent(babyApplication, TabHomeActivity.class);
                        startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        babyApplication.startActivity(startIntent);

                        stopSelf();
                    }else {
                        SystemUtils.startApp(babyApplication,allowPackName);
                    }
                }else if (rtaType == 4){
                    SystemUtils.sendForegActivity(babyApplication,TabHomeActivity.class);
                    stopSelf();
                }

            }
        })
        .timeout(3000)
        .start(this);
    }

    private boolean isHuaweiPay(String packageName) {
        if(TextUtils.isEmpty(packageName)){
            return false;
        }

        for (String allowPackName : allowHuaweiPayPackName){
            if(allowPackName.equals(packageName)){
                return true;
            }
        }

        return false;
    }

    private void stopChecker() {
        appChecker.stop();
    }
}
